package com.sky.controller.user;

import com.sky.dto.*;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.OrdersService;
import com.sky.utils.HttpClientUtil;
import com.sky.vo.OrderPaymentVO;
import com.sky.vo.OrderSubmitVO;
import com.sky.vo.OrderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user/order")
@Api(tags = "C端-订单相关接口")
@Slf4j
public class OrderController {

    @Autowired
    private OrdersService ordersService;
    /**
     * 接口描述：用户下单
     * 参数类型：请求体参数
     *      {
     *   "addressBookId": 0,
     *   "amount": 0,
     *   "deliveryStatus": 0,
     *   "estimatedDeliveryTime": "yyyy-MM-dd HH:mm:ss",
     *   "packAmount": 0,
     *   "payMethod": 0,
     *   "remark": "string",
     *   "tablewareNumber": 0,
     *   "tablewareStatus": 0
     *      }
     * 参数：OrderSubmitDTO
     * 返回值：Result<OrderSubmitVO>
     * 请求路径：/user/orders/submit
     * 请求方式：POST
     */
    @PostMapping("/submit")
    public Result<OrderSubmitVO> submit(@RequestBody OrdersSubmitDTO ordersSubmitDTO) {
        log.info("用户下单");
        OrderSubmitVO orderSubmitVO = ordersService.submit(ordersSubmitDTO);
        return Result.success(orderSubmitVO);
    }
    /**
     * 接口描述：查询历史订单
     * 参数类型：请求体参数
     *      {
     *          "page": 1,
     *          "pageSize": 10,
     *          "status": 0
     *      }
     * 请求路径：/user/orders/historyOrders
     * 请求方式：GET
     * 返回值：Result<PageResult>
     * 参数：OrdersPageQueryDTO
     */
    @GetMapping("/historyOrders")
    public Result<PageResult> historyOrders(OrdersPageQueryDTO ordersPageQueryDTO) {
        log.info("查询历史订单"+ordersPageQueryDTO);
        PageResult pageResult = ordersService.historyOrders(ordersPageQueryDTO);
        return Result.success(pageResult);
    }
    /**
     * 接口描述：取消订单
     * 参数类型：路径参数
     * 请求路径：/user/orders/cancel/{id}
     * 请求方式：PUT
     * 返回值：Result
     * 参数：id
     */
    @PutMapping("/cancel/{id}")
    public Result cancel(@PathVariable Long id, @RequestBody OrdersCancelDTO ordersCancelDTO) {
        log.info("取消订单");
        ordersService.cancel(id,ordersCancelDTO);
        return Result.success();
    }
    /**
     * 接口描述：查询订单详情
     * 参数类型：路径参数
     * 请求路径：/user/order/orderDetail/{id}
     * 请求方式：GET
     * 返回值：Result<OrderVO>
     * 请求参数：id
     */
    @GetMapping("/orderDetail/{id}")
    public Result<OrderVO> orderDetail(@PathVariable Long id) {
        log.info("查询订单详情");
        OrderVO orderVO = ordersService.orderDetailGetById(id);
        return Result.success(orderVO);
    }
    /**
     * 接口描述：再来一单
     * 参数类型：路径参数+请求体参数
     * 请求路径：/user/order/repetition/{id}
     * 请求方式：POST
     * 返回值：Result
     * 请求参数：id + ordersDTO
     */
    @PostMapping("/repetition/{id}")
    public Result repetition(@PathVariable Long id ) {
        ordersService.againOrders(id);
        return Result.success();
    }

    /**
     * 订单支付
     *
     * @param ordersPaymentDTO
     * @return
     */
    @PutMapping("/payment")
    @ApiOperation("订单支付")
    public Result<OrderPaymentVO> payment(@RequestBody OrdersPaymentDTO ordersPaymentDTO) throws Exception {
        log.info("订单支付：{}", ordersPaymentDTO);
        OrderPaymentVO orderPaymentVO = ordersService.payment(ordersPaymentDTO);
        log.info("生成预支付交易单：{}", orderPaymentVO);

        return Result.success(orderPaymentVO);
    }
    /**
     * 接口描述：用户催单
     * 参数类型：路径参数
     * 请求路径：/user/orders/reminder/{id}
     * 请求方式：GET
     * 返回值：Result
     * 请求参数：id
     */
    @GetMapping("/reminder/{id}")
    public Result reminder(@PathVariable Long id) {
        log.info("催单");
        ordersService.reminder(id);
        return Result.success();
    }
}
